package com.team.kgc.grade.controller;

import com.github.pagehelper.PageInfo;
import com.team.kgc.grade.entity.Grade;
import com.team.kgc.grade.entity.Student;
import com.team.kgc.grade.entity.StudentCondtion;
import com.team.kgc.grade.service.GradeService;
import com.team.kgc.grade.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class StudentController {
    //定义业务接口的属性
    @Autowired(required = false) //自动注入(省略setter和getter方法)  按类型区配
    private StudentService studentService;
    @Autowired
    private GradeService gradeService;


    @RequestMapping("showStudent")
    public String showStudent(Model model){     //查所有得学生
        //调用业务
        List<Student> list=studentService.getAllStudent();
        //填充model
        model.addAttribute("list",list);
        return "index";
    }

    @RequestMapping("showStudentByPage")    //连查分页
    public String showStudentByPage(StudentCondtion sc,Model model){
        PageInfo<Student> pageInfo=studentService.selectStuByCondtion(sc);
        model.addAttribute("pageInfo",pageInfo);
        return "index02";
    }

    //跳转到添加页面

    @RequestMapping("addStudent")  //添加
    public String addStudent(Student stu){    //添加学生
     int a= studentService.addStudent(stu);
     if (a>0){
         return "redirect:showStudentByPage";
     }else {
         return "redirect:showStudentByPage";
     }
    }

    @RequestMapping("deleteStudent")  //删除
    public String deleteStudent(Integer xh){    //删除学生
        int i = studentService.deleteStudent(xh);
        if (i>0){
            return "redirect:showStudentByPage";
        }else {
            return "redirect:showStudentByPage";
        }
    }

    @RequestMapping("selectOneStu")  //查单条
    public String selectOneStu(Integer xh,Model m){
        Student stu=studentService.selectOneStu(xh);
        List<Grade>li= this.gradeService.getAllGrade();
        m.addAttribute("stu",stu);
        m.addAttribute("gli",li);
        return "updateStudent";
    }

    @RequestMapping("updateStu")  //修改
    public String updateStu(Student student){
          int a= this.studentService.updateStu(student);
     if (a>0){
         return "redirect:showStudentByPage";
     }else {
         return "redirect:selectOneStu?xh="+student.getXh();
     }

    }

    @RequestMapping("queryStudentByCondtion")  //模糊查
    public String queryStudentByCondtion(StudentCondtion sc, Model model){
    PageInfo<Student>pageInfo=this.studentService.selectStuByCondtion(sc);
      model.addAttribute("pageInfo",pageInfo);
      model.addAttribute("sc",sc);
        return "index03";
    }

    @RequestMapping("getAllStudent")
    @ResponseBody
    public List<Student> getAllStudent(){
        List<Student> list = this.studentService.getAllStudent();
        return list;
    }

}

